package com.myjava.managesystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.myjava.managesystem.common.CustomException;
import com.myjava.managesystem.dto.SetmealDish;
import com.myjava.managesystem.dto.SetmealDto;
import com.myjava.managesystem.entity.Category;
import com.myjava.managesystem.entity.Dish;
import com.myjava.managesystem.entity.Setmeal;
import com.myjava.managesystem.mapper.SetmealMapper;
import com.myjava.managesystem.service.CategoryService;
import com.myjava.managesystem.service.DishService;
import com.myjava.managesystem.service.SetmealDishService;
import com.myjava.managesystem.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
@Transactional
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    SetmealDishService setmealDishService;

    @Autowired
    @Lazy
    CategoryService categoryService;

    @Autowired
    DishService dishService;

    @Override
    public void saveSetmealWithDish(SetmealDto setmealDto) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDto, setmeal);
        this.save(setmeal);
        List<SetmealDish> setmealDishList = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishList) {
            setmealDish.setSetmealId(setmeal.getId());
            setmealDishService.save(setmealDish);
        }
    }

    @Override
    public IPage<SetmealDto> getPageWithCategory(int page, int pageSize) {
        IPage<SetmealDto> iPage = new Page<>();
        LambdaQueryWrapper<Setmeal> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.orderByDesc(Setmeal::getUpdateTime);
        IPage<Setmeal> page1 = this.page(new Page<>(page, pageSize));
        BeanUtils.copyProperties(page1, iPage, "records");
        List<Setmeal> records = page1.getRecords();
        List<SetmealDto> list = new ArrayList<>();
        for (Setmeal s : records) {
            LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Category::getId, s.getCategoryId());
            SetmealDto dto = new SetmealDto();
            dto.setCategoryName(categoryService.getOne(wrapper).getName());
            BeanUtils.copyProperties(s, dto);
            list.add(dto);
        }
        iPage.setRecords(list);
        return iPage;
    }

    @Override
    public void removeSetmealWithDish(List<Long> ids) {
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Setmeal::getId, ids);
        wrapper.eq(Setmeal::getStatus, 1);
        if (this.count(wrapper) > 0) {
            throw new CustomException("套餐正在售卖无法删除");
        }
        this.removeByIds(ids);
        LambdaQueryWrapper<SetmealDish> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.in(SetmealDish::getSetmealId, ids);
        setmealDishService.remove(wrapper1);

    }

    //待优化!!
    @Override
    public void updateStatusWithDish(int st, List<Long> ids) {
        if(st>0){
            LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SetmealDish::getSetmealId,ids);
            List<SetmealDish> setmealDishList = setmealDishService.list();
            Set<Long> dishIds = new HashSet<>();
            for(SetmealDish setmealDish:setmealDishList){
                dishIds.add(setmealDish.getDishId());
            }
            LambdaQueryWrapper<Dish> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.in(Dish::getId,dishIds);
            wrapper1.eq(Dish::getStatus,0);
            if(dishService.count(wrapper1)>0){
                throw new CustomException("当前选择套餐的部分商品已停售，无法起售该套餐");
            }
        }
        LambdaQueryWrapper<Setmeal> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.in(Setmeal::getId,ids);
        List<Setmeal> setmealList = this.list(wrapper2);
        for(Setmeal setmeal:setmealList){
            setmeal.setStatus(st);
        }
        this.updateBatchById(setmealList);
    }
}
